package com.bulletelc.apollopre.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;

import java.util.Map;

import static android.content.Context.MODE_PRIVATE;

/**
 * author: lhl
 * time  : 2017/10/22
 * desc  : SharedPreferences(偏好设置)工具类
 */

public final class SharedPreferencesUtil {

    private static final String SP_FILE_NAME = "apollo_pre";

    private static SharedPreferences sp;

    private SharedPreferencesUtil() {
        //empty
    }

    /**
     * 实例化SharedPreferences
     * @param context
     */
    private static void init(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
        }
    }

    public static void putInt(Context context, String key, int value) {
        init(context);
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt(Context context, String key) {
        init(context);
        return sp.getInt(key, 0);
    }

    public static void putLong(Context context, String key, long value) {
        init(context);
        sp.edit().putLong(key, value).commit();
    }

    public static long getLong(Context context, String key) {
        init(context);
        return sp.getLong(key, 0l);
    }

    public static void putFloat(Context context, String key, float value) {
        init(context);
        sp.edit().putFloat(key, value).commit();
    }

    public static Float getFloat(Context context, String key) {
        init(context);
        return sp.getFloat(key, 0f);
    }

    public static void putBoolean(Context context, String key, boolean value) {
        init(context);
        sp.edit().putBoolean(key, value).commit();
    }

    public static Boolean getBoolean(Context context, String key) {
        init(context);
        return sp.getBoolean(key, false);
    }

    public static void putString(Context context, String key, String value) {
        init(context);
        sp.edit().putString(key, value).commit();
    }

    public static String getString(Context context, String key) {
        init(context);
        return sp.getString(key, "");
    }

    /**
     * 删除指定数据
     * @param context
     * @param key
     */
    public static void remove(Context context, String key) {
        init(context);
        sp.edit().remove(key).commit();
    }

    /**
     * 删除所有SharedPreferences存储数据
     * @param context
     */
    public static void clear(Context context) {
        init(context);
        sp.edit().clear().commit();
    }

    /**
     * 获取所有数据的键值对
     * @param context
     * @return
     */
    public static Map<String, ?> getAll(Context context) {
        init(context);
        Map<String, ?> map = sp.getAll();
        return map;
    }

    /**
     * 检查key对应的数据是否存在
     * @param context
     * @param key
     * @return
     */
    public static boolean contains(Context context,String key) {
        init(context);
        return sp.contains(key);
    }

    /**
     * 获取其他应用的SharedPreferences对象
     * @param context
     * @param otherPackageName 其他应用的包名
     * @param otherKey  SharedPreferences的key值
     * @return sharedPreferences实例 or null
     */
    public static SharedPreferences getOtherSharedPreferences(Context context,String otherPackageName,String otherKey){
        SharedPreferences sharedPreferences=null;
        try {
           Context otherContext=context.createPackageContext(otherPackageName, Context.CONTEXT_IGNORE_SECURITY);
            sharedPreferences=otherContext.getSharedPreferences(otherKey,Context.MODE_WORLD_READABLE);
            return sharedPreferences;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return sharedPreferences;
        }
    }

}
